#-*- coding:utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
import tempfile

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

FLAGE = True

def deepnn(x):
    with tf.name_scope('reshape'):
        x_image = tf.reshape(x,[-1,28,28,1])

    # 第一层卷积层，卷积核为5*5，生成32个feature map
    with tf.name_scope("conv1"):
        W_conv1 = weight_variable([5,5,1,32])
        b_conv1 = bias_variable([32])
        h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)

    # 第一层池化层，下采样2
    with tf.name_scope("pool1"):
        h_pool1 = max_pool_2x2(h_conv1)

    # 第二层卷积层，卷积核为5*5，生成64个feature map
    with tf.name_scope("conv2"):
        W_conv2 = weight_variable([5,5,32,64])
        b_conv2 = bias_variable([64])
        h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)

    # 第二层池化层，下采样2
    with tf.name_scope("pool2"):
        h_pool2 = max_pool_2x2(h_conv2)

    # 将池化的结果转化为一维向量，进入全连接
    h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])

    # 第一层全连接层
    with tf.name_scope("fc1"):
        W_fc1 = weight_variable([7*7*64,1000])
        b_fc1 = bias_variable([1000])
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)

    # dropout层，训练时候随机让某些影藏层节点权重不工作
    with tf.name_scope("dropout"):
        keep_prob = tf.placeholder(tf.float32)
        h_fc1_dropout = tf.nn.dropout(h_fc1,keep_prob)

    # 第二层全连接层，1024个features 和 10 个features全连接
    with tf.name_scope("fc2"):
        W_fc2 = weight_variable([1000,10])
        b_fc2 = bias_variable([10])
        h_fc2 = tf.matmul(h_fc1_dropout,W_fc2)+b_fc2

    return h_fc2,keep_prob

# 卷积
def conv2d(x, Weight):
    return tf.nn.conv2d(x,Weight,[1,1,1,1],padding="SAME")
# 池化
def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
# 权重
def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)
# 偏置
def bias_variable(shape):
    initial = tf.constant(0.1, dtype=tf.float32, shape=shape)
    return tf.Variable(initial)